<script>
    $(document).ready(function () {
        //添加
        $("#add-sub").click(function () {
            var idc_name = $("#idc-name").val();
            var mg_user = $("#mg-user").val();
            var email = $("#email").val();
            var mobile = $("#mobile").val();
            $.ajax({
                type:"POST",
                async:false,
                url:"/cmdb/addidc/",
                data:{"idc_name":idc_name,"mg_user":mg_user,"email":email,"mobile":mobile},
                dataType : "json",
                success:function (data) {
                    $("#addModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //获取修改内容
        $("td>a[name='edit-idc']").click(function () {
            var id = $(this).attr("edit_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/cmdb/ididc/",
                data:{"id":id },
                dataType: "json",
                success:function (data) {
                    $("#edit-idc-name").val(data.IdcName);
                    $("#edit-mg-user").val(data.MgUser);
                    $("#edit-email").val(data.Email);
                    $("#edit-mobile").val(data.Mobile);
                    $("#edit-sub").attr("edit_id",data.ID);
                    $("#editModal").modal("show");
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });

        //提交修改内容
        $("#edit-sub").click(function () {
            var id = $(this).attr("edit_id");
            var idc_name = $("#edit-idc-name").val();
            var mg_user = $("#edit-mg-user").val();
            var email = $("#edit-email").val();
            var mobile = $("#edit-mobile").val();
            $.ajax({
                type:"PUT",
                async:false,
                url:"/cmdb/editidc/",
                data:{"id":id,"idc_name":idc_name,"mg_user":mg_user,"email":email,"mobile":mobile},
                dataType : "json",
                success:function (data) {
                    $("#editModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //删除
        $("td>a[name='del-idc']").click(function () {
            var id = $(this).attr('del_id');
            var statu = confirm("是否确认删除!");
            if (statu==true)
            {
                $.ajax({
                    type: "post",
                    async:false,
                    url: "/cmdb/delidc/",
                    data: {"id":id},
                    dataType : "json",
                    success: function(data) {
                        spop({
                            template:data,
                            style: 'warning',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()",2100);
                    },
                    error:function (data) {
                        spop({
                            template:data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //搜索
        $("#search-key").bind('change',function () {
            var key = $(this).val();
            if (key != "" ) {
                var url = "/cmdb/searchidc/" + key + "/";

            }else{
                var url = "/cmdb/idc/";
            }
            $("#search-a").attr("href", url);
        });

    })
</script>
